﻿//服务器内存管理
var system = function() {
	this._mem = new java.lang.management.ManagementFactory.getOperatingSystemMXBean();
	this._lang = new java.lang.Runtime.getRuntime();
	//释放内存
	this.gc = function() {
		var _kb = 1024;
		var _freed = 0;
		//var _total = this._mem.getTotalPhysicalMemorySize() / _kb;
		//var _free = _freed = this._mem.getFreePhysicalMemorySize() / _kb;
		var _total = this._lang.totalMemory() / _kb;
		var _free = this._lang.freeMemory() / _kb;
		var _remain = _freed = _total - _free;		
		__out.println("--- [ GC Memory ]----------------------------------------------------");
		__out.println(" Before  Total: " + _total + "kb  Free: " + _free + "kb     Used: " + _remain + "kb");
		
		this._lang.gc();
		//new java.lang.System.gc();
		_free = this._lang.freeMemory() / _kb;
		_remain = _total - _free;
		__out.println(" Now     Total: " + _total + "kb  Free: " + _free + "kb     Used: " + _remain + "kb");
		__out.println("\n");		
	}
	//查看内存情况
	this.memInfo = function() {
		var _kb = 1024;
		/*__out.println("==============================Memory Info ==========================");
		var _str = " Total : " + this._mem.getTotalPhysicalMemorySize()/_kb +"kb";
		_str += "  Free : " + this._mem.getFreePhysicalMemorySize()/_kb+"kb";
		_str += "  Used : " + (this._mem.getTotalPhysicalMemorySize() - this._mem.getFreePhysicalMemorySize()) / _kb + "kb";		
		__out.println(_str);
		__out.println("==============================End Memory ===========================");
		*/
		__out.println("--- [ Memory Info ]----------------------------------------------------");		
		var _str = " Total : " + this._lang.totalMemory()/_kb +"kb";
		_str += "  Free : " + this._lang.freeMemory()/_kb+"kb";
		_str += "  Used : " + (this._lang.totalMemory() - this._lang.freeMemory()) / _kb + "kb";		
		__out.println(_str);
		__out.println("\n");		
	}
}